//搭建本地的服务器
const express = require("express");//引入express
//引入mongoose
const mongoose = require("mongoose");
//引入body-parser
const bodyParser=require("body-parser");
//引入passport
const passport=require("passport");
//实例化一个App
const app = express();


//引入users.js
const users=require("./routes/api/users")
const profiles=require("./routes/api/profiles");

// 配置 DB,引入mongoose
const db = require("./config/keys").mongoURI;


//使用body-parser中间件
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());

 

//连接数据库
mongoose.connect(db,{ useNewUrlParser: true, useUnifiedTopology: true })
            .then(()=>console.log("MongoDB Connected"))
            .catch(err=>console.log(err));


//初始化passport
app.use(passport.initialize());

require("./config/passport")(passport);

// 设置路由
app.get("/", (req, res) => {
    res.send("Hello world");
})

//使用routes
app.use("/api/users",users);
app.use("/api/profiles",profiles);

//端口号
const port = process.env.PORT || 5000;

//监听
app.listen(port, () => {
    console.log(`Server running on port ${port}`);
}) 